package zhengyanze.com.aipoemmax.guide.fragment.filter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.Constant;
import zhengyanze.com.aipoemmax.guide.activity.MingActivity;
import zhengyanze.com.aipoemmax.guide.adapter.MainAdapter;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean;
import zhengyanze.com.aipoemmax.guide.utils.LogUtils;


/**
 * 古文观止
 * Created by MrZheng on 2017/7/24.
 */

public class GuwenFragment extends Fragment {
    public static final String TAG = "GuwenFragment";
    @BindView(R.id.rv_guwen)
    RecyclerView rvGuwen;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;

    ArrayList<PoemBean> mPoemBeans;
    private MainAdapter mainAdapter;
    String moreLoad;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guwen, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtils.e("hehe","onViewCreated() + GuwenFragment");

        mPoemBeans = new ArrayList<>();
        initView();
        initData(Constant.URL.GUSHI_GUWENGUANZHI);
    }

    public void initView() {
        rvGuwen.setLayoutManager(new LinearLayoutManager(getContext()));
        mainAdapter = new MainAdapter(mPoemBeans);
        rvGuwen.setAdapter(mainAdapter);

        //加载更多
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshLayout.finishLoadmore(2000);
                if (moreLoad != null) {
                    String moreU = Constant.URL.GUSHI_BASE + moreLoad;
                    initData(moreU);
                }
            }
        });
    }

    //获取
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

        }
    };

    public void initData(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document document = Jsoup.connect(url).get();
                    Element main3 = document.getElementsByClass("main3").first();
                    Element left = main3.getElementsByClass("left").first();
                    Elements sons = left.getElementsByClass("sons");

                    ArrayList<PoemBean> tempArray = new ArrayList<PoemBean>();
                    for (Element e : sons) {
                        Element cont = e.getElementsByClass("cont").first();
                        Element p = cont.getElementsByTag("p").first();
                        Element a = p.getElementsByTag("a").first();
//                        Log.e(TAG, "标题是: " + a.text());
                        String url = Constant.URL.GUSHI_BASE + a.attr("href").toString();
//                        Log.e(TAG, "标题超链接是: " + url);

                        Element source = cont.getElementsByClass("source").first();
//                        Log.e(TAG, "作者是: " + source.text());

                        Element contson = cont.getElementsByClass("contson").first();
//                        Log.e(TAG, "内容是: " + contson.text());

                        PoemBean poem = new PoemBean(a.text(),source.text(),contson.text(),url);
                        tempArray.add(poem);
                    }
                    //获取加载更多链接
                    Element pages = left.getElementsByClass("pages").last();
                    Element linkA = pages.getElementsByTag("a").last();
//                    Log.e(TAG, "超联机: " + linkA.attr("href").toString());
                    if (linkA == null) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getContext(), "没有更多结果了", Toast.LENGTH_SHORT).show();
                            }
                        });
                        moreLoad = null;
                    }else{
                        moreLoad = linkA.attr("href").toString();
                    }

                    mPoemBeans.addAll(tempArray);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mainAdapter.notifyDataSetChanged();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
